﻿using System;
using System.Windows.Forms;
using Microsoft.Win32;

namespace WinRun
{
    public partial class FormSettings : Form
    {
        private bool writeKey = false;

        public FormSettings()
        {
            InitializeComponent();
        }

        private void FormSettings_Load(object sender, EventArgs e)
        {
            /** Set the initial values **/
            LblBackColor.BackColor = Properties.Settings.Default.CommandBackcolor;
            LblForeColor.BackColor = Properties.Settings.Default.CommandForecolor;
            TxtFont.Text = Properties.Settings.Default.CommandFont.Name;
            TxtFontSize.Text = Properties.Settings.Default.CommandFont.SizeInPoints.ToString();
            LblSampleText.Font = Properties.Settings.Default.CommandFont;
            LblSampleText.BackColor = Properties.Settings.Default.CommandBackcolor;
            LblSampleText.ForeColor = Properties.Settings.Default.CommandForecolor;
            ChkTopmost.Checked = Properties.Settings.Default.setTopmost;
            ChkChase.Checked = Properties.Settings.Default.followCursor;
            ChkHideAfter.Checked = Properties.Settings.Default.HideAfterExecute;

            /** check the registry to see if we are set to start with windows **/
            ChkStartup.Checked = GetAutoStart();
        }

        /// <summary>
        /// Used to set the background color of the command box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdChangeBackcolor_Click(object sender, EventArgs e)
        {
            /** open a color dialog to allow user to select **/
            var cDlg = new ColorDialog()
                {
                    AnyColor = true,
                    AllowFullOpen = true,
                    SolidColorOnly = true,
                    Color = Properties.Settings.Default.CommandBackcolor
                };

            DialogResult res = cDlg.ShowDialog();

            /** if they didnt press cancel set the color in the examples **/
            if (res != DialogResult.Cancel)
            {

                LblBackColor.BackColor = cDlg.Color;
                LblSampleText.BackColor = cDlg.Color;
            }
        }

        /// <summary>
        /// Used to set the font color of the command box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdChangeForecolor_Click(object sender, EventArgs e)
        {
            var cDlg = new ColorDialog()
                {
                    AnyColor = true,
                    AllowFullOpen = true,
                    SolidColorOnly = true,
                    Color = Properties.Settings.Default.CommandForecolor
                };

            DialogResult res = cDlg.ShowDialog();

            /** if they didnt press cancel set the color in the examples **/
            if (res == DialogResult.OK)
            {
                LblForeColor.BackColor = cDlg.Color;
                LblSampleText.ForeColor = cDlg.Color;

            }
        }

        /// <summary>
        /// Used to change command box font properties
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdChangeFont_Click(object sender, EventArgs e)
        {
            var fDia = new FontDialog()
                {
                    Font = Properties.Settings.Default.CommandFont
                };


            DialogResult res = fDia.ShowDialog();
            if (res == DialogResult.OK)
            {
                LblSampleText.Font = fDia.Font;
                TxtFont.Text = fDia.Font.Name;
                TxtFontSize.Text = fDia.Font.SizeInPoints.ToString();
            }
        }

        /// <summary>
        /// Creates the autostart key if true, deletes it if false
        /// </summary>
        /// <param name="createKey">true = write, false = delete</param>
        private void SetAutoStart(bool createKey)
        {
            var rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",true);


            /** no need to write the key if it alread exists **/
            if (createKey == true && GetAutoStart() == false)
            {
                /** create the key **/
                rk.SetValue("WinRun", Application.ExecutablePath.ToString());
                return; 
            }

            /** get rid of autostart entry **/
            rk.DeleteValue("WinRun", false);
        }

        /// <summary>
        /// Get the value in the autostart key
        /// </summary>
        /// <returns>true if key exists (on), false if it doesn't (off)</returns>
        private bool GetAutoStart()
        {
            var rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            if (rk.GetValue("WinRun") == null)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// sets the changed flag. we dont want to mess with reg keys if we dont need to
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChkStartup_CheckedChanged(object sender, EventArgs e)
        {
            writeKey = true;
        }

        /// <summary>
        /// Save settings to the properties file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdOk_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.CommandBackcolor = LblBackColor.BackColor;
            Properties.Settings.Default.CommandForecolor = LblForeColor.BackColor;
            Properties.Settings.Default.CommandFont = LblSampleText.Font;
            Properties.Settings.Default.setTopmost = ChkTopmost.Checked;
            Properties.Settings.Default.followCursor = ChkChase.Checked;
            Properties.Settings.Default.HideAfterExecute = ChkHideAfter.Checked ;
            Properties.Settings.Default.Save();
            Properties.Settings.Default.Reload();

            if (writeKey)
            {
               SetAutoStart(ChkStartup.Checked);  
            }
            Close();
        }

        /// <summary>
        /// disreguard changes and close dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    
    }
}
